﻿using Domain.Entity;
using DtoFw;
using DtoFw.Query;
using Repository.Base;
using Service;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Util;

namespace Repository
{
    public class CasePhysicalPanDianRepository : BaseRepository<CasePhysicalPanDianEntity>
    {
        private readonly SnowFlakeGenerator worker;
        public CasePhysicalPanDianRepository(SnowFlakeGenerator snowFlakeGenerator) : base(snowFlakeGenerator)
        {
            this.worker = snowFlakeGenerator;
        }

        public bool Update(CasePhysicalPanDianEntity inf)
        {
            return base.GetDb().Updateable(inf).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<Pager<object>> Pager(CaseQueryDto query)
        {
            int total = 0;
            BaseGenericResult<Pager<object>> baseGeneric = new BaseGenericResult<Pager<object>>();
            baseGeneric.Data = new Pager<object>();

            var pager = base.GetDb().Queryable<CaseEntity, CasePhysicalPanDianEntity>((x, y) =>
                    new JoinQueryInfos(JoinType.Inner, x.Id == y.CaseId))
                .Where((x, y) => x.IsDeleted == false && y.IsDeleted == false)
                .WhereIF(!string.IsNullOrWhiteSpace(query.CaseNo), (x) => x.CaseNo == query.CaseNo) 
                .WhereIF(!string.IsNullOrWhiteSpace(query.CaseName), (x) => x.CaseTypeCode == (query.CaseName)) 
                .WhereIF((query.CaseTimeStart.HasValue && query.CaseTimeEnd.HasValue), (x) => (x.CreateTime > query.CaseTimeStart && x.CreateTime < query.CaseTimeEnd))
                .OrderBy(z => z.CreateTime, OrderByType.Desc)
                .Select((x, y) => new
                {
                    y.Id,
                    x.CaseNo,
                    CaseName = x.CaseName,
                    y.Title,
                    y.CaseId,
                    y.Status,
                    y.Remark,
                    y.Attrs,
                    y.CreateTime,
                    y.Creator,
                    y.Modifyer,
                    y.ModifyerTime,
                    y.IsDeleted
                })
                .ToPageList(query.Page, query.PageSize, ref total).ToList<dynamic>();
            baseGeneric.Data.Data = pager;
            baseGeneric.Data.TotalCount = total;
            return baseGeneric;
        }
    
        #region 盘点任务
        public List<PanDianTaskDetailEntity> GetTaskDetails(List<long> ids)
        {
            var sql = $@"select `No` as epc,`Name` as name,Location as address
                           from case_physical_evidence 
                          where id in({string.Join(',', ids)}) ";
            return base.GetDb().SqlQueryable<PanDianTaskDetailEntity>(sql).ToList();
        }


        #endregion 盘点任务

    }
}
